package cn.com.egova.materialdesignexample.activity;

import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;

import cn.com.egova.materialdesignexample.R;

public class FloatingActionButtonActivity extends AppCompatActivity implements View.OnClickListener{
    private CoordinatorLayout cl;
    private LinearLayout ll_fab;
    private FloatingActionButton fab1;
    private FloatingActionButton fab2;
    private FloatingActionButton fab3;
    private boolean isShowing=false;  //3个FloatingActionButton是否全部在显示状态.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_floating_action_button);

        initToolbar();

        initView();
    }

    private void initToolbar(){
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("FloatingActionButton");
        toolbar.setNavigationIcon(R.mipmap.icon_back);
        toolbar.setNavigationOnClickListener(new NavigationView.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    private void initView(){
        cl = (CoordinatorLayout) findViewById(R.id.coordinatorLayout);
        ll_fab = (LinearLayout) findViewById(R.id.ll_fab);
        fab1 = (FloatingActionButton) findViewById(R.id.fab1);
        fab2 = (FloatingActionButton) findViewById(R.id.fab2);
        fab3 = (FloatingActionButton) findViewById(R.id.fab3);

        fab1.setOnClickListener(this);
        fab2.setOnClickListener(this);
        fab3.setOnClickListener(this);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.menu_fab,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case  R.id.show:
                Snackbar snackbar = Snackbar.make(cl,"show snackbar",Snackbar.LENGTH_LONG);
                snackbar.setAction("Undo", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Snackbar.make(cl,"undo success!",Snackbar.LENGTH_LONG).show();
                    }
                });
                snackbar.show();
                break;
        }
        return true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case  R.id.fab1:
                isShowing = !isShowing;
                if(isShowing){
                    collapse();
                }else{
                    expand();
                }
                break;
            case  R.id.fab2:
                Snackbar.make(cl,"you had touched fab2",Snackbar.LENGTH_LONG).show();
                break;
            case  R.id.fab3:
                Snackbar.make(cl,"you had touched fab3",Snackbar.LENGTH_LONG).show();
                break;
        }
    }


    private void expand(){
        fab1.setImageResource(R.mipmap.small);
        fab2.setVisibility(View.VISIBLE);
        fab3.setVisibility(View.VISIBLE);
    }

    private void collapse(){
        fab1.setImageResource(R.mipmap.ic_launcher_round);
        fab2.setVisibility(View.GONE);
        fab3.setVisibility(View.GONE);
    }
}
